package jgl.vajra.kingkong.part.vehicle.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.databinding.ActivityOrderRemarksBinding;
import jgl.vajra.kingkong.event.OrderRemarksEvent;
import jgl.vajra.kingkong.part.vehicle.contract.OrderRemarksContract;
import jgl.vajra.kingkong.part.vehicle.viewModel.OrderRemarksViewModel;
import jgl.vajra.kingkong.utils.PictureUtils;
import jgl.vajra.kingkong.widget.bean.MultiPicChooseBean;
import jgl.vajra.kingkong.widget.giide.GlideImageLoader;
import jgl.vajra.kingkong.widget.interfaces.ImagePickerListener;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.utils.PickFileToolsV3;

@CreateViewModel(OrderRemarksViewModel.class)
public class OrderRemarksActivity extends BaseMVVMActivity<OrderRemarksViewModel, ActivityOrderRemarksBinding> implements OrderRemarksContract.View {

    private PickFileToolsV3 pickFileTools;
    private String videoPath;
    String videoShortThumbName = "videoShortThumb.png";
    private String serviceLife;
    private String WorkingYears;
    private String accommodation;
    private String canteen;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_remarks;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);


        mBinding.bSeekBar.setOnSeekBarChangeListener((leftProgress, rightProgress) -> {
            serviceLife = leftProgress + "～" + rightProgress;
            mBinding.tvUsageTime.setText(leftProgress + "年～" + rightProgress + "年");
        });
        mBinding.workingYearsBSeekBar.setOnSeekBarChangeListener((leftProgress, rightProgress) -> {
            WorkingYears = leftProgress + "～" + rightProgress;
            mBinding.tvWorkingYearsTime.setText(leftProgress + "年～" + rightProgress + "年");
        });

        mBinding.etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int length = editable.toString().length();
                mBinding.tvRemarkNum.setText(length + "/80");
            }
        });

    }

    @Override
    public void onSelectVideo() {
        if (pickFileTools == null)
            pickFileTools = PickFileToolsV3.init(this);
        pickFileTools.pickVideo(new ImagePickerListener() {
            @Override
            public void onPickFileList(List<LocalMedia> imgs, int tag) {
                super.onPickFileList(imgs, tag);
                for (int i = 0; i < imgs.size(); i++) {
                    String videoPath1 = "";
                    if (TextUtils.isEmpty(imgs.get(i).getAndroidQToPath())) {
                        videoPath1 = imgs.get(i).getRealPath();
                    } else {
                        videoPath1 = imgs.get(i).getAndroidQToPath();
                    }
                    Logger.d("videoPath1" + videoPath1);
                }
                uploadVideo(imgs.get(0));
            }
        });
    }

    @Override
    public void onDeleteVideo() {
        mBinding.ivVideoDelete.setVisibility(View.GONE);
        mBinding.ivVideoImage.setVisibility(View.GONE);
        videoPath = "";
        //this.localMedia = null;

    }

    //宿舍不提供
    @Override
    public void onDormitoryNoAvailable() {
        accommodation = "不提供，需自己解决";
        mBinding.ivDormitoryNoAvailable.setSelected(true);
        mBinding.ivDormitoryAvailable.setSelected(false);
        mBinding.ivDormitoryFree.setSelected(false);
    }

    //宿舍提供
    @Override
    public void onDormitoryAvailable() {
        accommodation = "可提供，需自费";
        mBinding.ivDormitoryAvailable.setSelected(true);
        mBinding.ivDormitoryNoAvailable.setSelected(false);
        mBinding.ivDormitoryFree.setSelected(false);
    }

    //宿舍免费
    @Override
    public void onDormitoryFree() {
        accommodation = "免费提供";
        mBinding.ivDormitoryFree.setSelected(true);
        mBinding.ivDormitoryNoAvailable.setSelected(false);
        mBinding.ivDormitoryAvailable.setSelected(false);
    }

    //食堂不提供
    @Override
    public void onCanteenNoAvailable() {
        canteen = "不提供，需自己解决";
        mBinding.ivCanteenNoAvailable.setSelected(true);
        mBinding.ivCanteenAvailable.setSelected(false);
        mBinding.ivCanteenFree.setSelected(false);
    }

    //食堂提供
    @Override
    public void onCanteenAvailable() {
        canteen = "可提供，需自费";
        mBinding.ivCanteenAvailable.setSelected(true);
        mBinding.ivCanteenNoAvailable.setSelected(false);
        mBinding.ivCanteenFree.setSelected(false);
    }

    //食堂免费
    @Override
    public void onCanteenFree() {
        canteen = "免费提供";
        mBinding.ivCanteenFree.setSelected(true);
        mBinding.ivCanteenNoAvailable.setSelected(false);
        mBinding.ivCanteenAvailable.setSelected(false);
    }

    public void uploadVideo(LocalMedia localMedia) {
        videoPath = "";
        if (TextUtils.isEmpty(localMedia.getAndroidQToPath())) {
            videoPath = localMedia.getRealPath();
        } else {
            videoPath = localMedia.getAndroidQToPath();
        }
        Logger.d("videoPath" + videoPath);
        mBinding.ivVideoImage.setVisibility(View.VISIBLE);
        mBinding.ivVideoDelete.setVisibility(View.VISIBLE);
        onImageDisplayRadius();

      /*  isChooseVideo = false;
        videoNetUrl = "";
        fileName = File.separator + "VID_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
*/
    }

    private void onImageDisplayRadius() {
        GlideImageLoader.onDisplayRadius(mBinding.ivVideoImage, videoPath, R.dimen.dp_5);
       /* Glide
                .with(mBinding.ivVideoImage.getContext()).asBitmap()
                .load(videoPath)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .centerCrop()
                .apply(GlideImageLoader.getRequestOptions().transform(new CornersTransform(UIUtils.getContext(), R.dimen.dp_5)))
                .listener(new RequestListener<Bitmap>() {

                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Bitmap> target, boolean b) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap bitmap, Object o, Target<Bitmap> target, DataSource dataSource, boolean b) {
                        BitmapUtils.saveImage(bitmap, videoShortThumbName);
                         GlideImageLoader.onDisplayRadius(mBinding.ivVideoImage, videoPath, R.dimen.dp_5);

                        return false;
                    }
                })
                .into(mBinding.ivVideoImage);*/


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (pickFileTools != null) {
            pickFileTools.onActivityResult(requestCode, resultCode, data);
        }
        mBinding.multiPicVideoView.onActivityResult(requestCode, resultCode, data);
    }

    //确定
    @Override
    public void onDetermine() {

        String remark = mBinding.etRemark.getText().toString().trim();

        Map<String, Object> map = new HashMap<>();
        map.put("useYear", serviceLife);
        map.put("operateExperience", WorkingYears);
        map.put("otherRequirements", remark);

        map.put("supplyHostel", accommodation);
        map.put("supplyFood", canteen);

        List<MultiPicChooseBean> pathList = mBinding.multiPicVideoView.getPathList();
        Map<String, String> pictureMap = new HashMap<>();
        Map<String, String> videoMap = new HashMap<>();
        for (int i = 0; i < pathList.size(); i++) {
            if (pathList.get(i).type == 1) {
                String base64Picture = PictureUtils.bitmapToString(pathList.get(i).imgUrlString);
                // picture.add(i + ".jpeg :" + base64Picture);
                Logger.d("图片" + base64Picture);
                pictureMap.put(i + ".jpeg", base64Picture);
            } else if (pathList.get(i).type == 2) {
                String base64Picture = PictureUtils.fileToBase64(pathList.get(i).imgUrlString);
                // picture.add(i + ".jpeg :" + base64Picture);
                Logger.d("视频" + base64Picture);
                videoMap.put(i + ".mp4", base64Picture);
            }

        }
        if (pictureMap.size() != 0) {
            map.put("picture", pictureMap);
        }
        if (videoMap.size() != 0) {
            map.put("video", videoMap);
        }
        EventBus.getDefault().post(new OrderRemarksEvent(map));
        finish();
    }
}
